package com.hsgene.user.service.impl;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.user.condition.DepartmentCondition;
import com.hsgene.user.domain.Department;
import com.hsgene.user.dto.orginazation.DepartmentDto;
import com.hsgene.user.persistence.DepartmentRepository;
import com.hsgene.user.persistence.UserRepository;
import com.hsgene.user.service.DepartmentService;
import com.hsgene.user.service.api.AuthService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @projectName: hdas-geneshop-server
 * @package: com.hsgene.order.service.impl
 * @author: maodi
 * @createDate: 2018/11/5 10:14
 * @version: 1.0
 * Copyright: Copyright (c) 2018
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    DepartmentRepository departmentRepository;

    @Resource
    UserRepository userRepository;

    /**
     * @param condition 部门条件
     * @return int
     * @description 部门条件查询数量
     * @author maodi
     * @createDate 2018/11/6 16:42
     */
    @Override
    public int queryDepartmentCountByCondition(DepartmentCondition condition) {
        return departmentRepository.queryDepartmentCountByCondition(condition);
    }

    /**
     * @param condition 部门条件
     * @return List<DepartmentDto>
     * @description 部门条件查询
     * @author maodi
     * @createDate 2018/11/6 16:42
     */
    @Override
    public List<DepartmentDto> queryDepartmentByCondition(DepartmentCondition condition) {
        return departmentRepository.queryDepartmentByCondition(condition);
    }

    /**
     * @param id 部门id
     * @return java.util.List<com.hsgene.user.dto.orginazation.DepartmentDto>
     * @description 部门根据id查询
     * @author maodi
     * @createDate 2018/11/6 16:44
     */
    @Override
    public List<DepartmentDto> queryDepartmentById(String id) {
        return departmentRepository.queryDepartmentById(id);
    }

    /**
     * @param departmentName 部门
     * @param companyId      公司id
     * @return int
     * @description 部门名字，公司id查询数量
     * @author maodi
     * @createDate 2018/11/6 18:04
     */
    @Override
    public int queryDepartmentCountByName(String departmentName, String companyId) {
        return departmentRepository.queryDepartmentCountByName(departmentName, companyId);
    }

    /**
     * @param departmentName 部门名称
     * @param companyId      公司id
     * @param departmentId   部门id
     * @return int
     * @description 部门名字，公司id除本身外查询数量
     * @author maodi
     * @createDate 2018/11/7 15:07
     */
    @Override
    public int queryDepartmentCountByNameExcludeSelf(String departmentName, String companyId, String departmentId) {
        return departmentRepository.queryDepartmentCountByNameExcludeSelf(departmentName, companyId, departmentId);
    }

    /**
     * @param companyId 公司id
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @description 根据公司id查询部门下拉框数据源
     * @author maodi
     * @createDate 2018/11/7 9:14
     */
    @Override
    public List<Map<String, Object>> selectDepartment(String companyId) {
        return departmentRepository.selectDepartment(companyId);
    }

    /**
     * @param departmentId 部门id
     * @param companyId    公司id
     * @return int
     * @description 根据部门id和公司id查询数量
     * @author maodi
     * @createDate 2018/11/7 18:26
     */
    @Override
    public int queryCountByDepartmentIdAndCompanyId(String departmentId, String companyId) {
        return departmentRepository.queryCountByDepartmentIdAndCompanyId(departmentId, companyId);
    }

    /**
     * @param companyId 公司id
     * @return int
     * @description 查询公司id在部门中使用数量
     * @author maodi
     * @createDate 2018/11/7 18:41
     */
    @Override
    public int queryCountByCompanyId(String companyId) {
        return departmentRepository.queryCountByCompanyId(companyId);
    }

    /**
     * @param department 部门
     * @return void
     * @description 部门新增
     * @author maodi
     * @createDate 2018/11/6 16:44
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int insertDepartment(Department department) {
        int count = departmentRepository.insertDepartment(department);
        return count;
    }

    /**
     * @param department 部门
     * @return void
     * @description 部门修改
     * @author maodi
     * @createDate 2018/11/6 16:45
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int updateDepartment(Department department) {
        String companyId = department.getCompanyId();
        String departmentId = department.getId();
        userRepository.updateDepartmentNameByDepartmentId(department.getDepartmentName(), departmentId);
        departmentRepository.updatePositionCompany(companyId, departmentId);
        return departmentRepository.updateDepartment(department);
    }

    /**
     * @param department 部门
     * @return void
     * @description 部门删除
     * @author maodi
     * @createDate 2018/11/6 16:45
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int deleteDepartment(Department department) {
        int count = departmentRepository.deleteDepartment(department);
        return count;
    }

    /**
     * @param departmentIds 部门id数组
     * @return int
     * @description 部门批量删除
     * @author maodi
     * @createDate 2018/11/13 17:04
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int deleteDepartmentMulti(String[] departmentIds) {
        return departmentRepository.deleteDepartmentMulti(departmentIds, new Date());
    }

    /**
     * @param departmentId 部门id
     * @return java.lang.Integer
     * @description 查询部门id在系统用户表中使用数量
     * @author maodi
     * @createDate 2018/11/29 11:34
     */
    @Override
    public Integer queryOperationAccountCountByDepartmentId(String departmentId) {
        return departmentRepository.queryOperationAccountCountByDepartmentId(departmentId);
    }
}
